﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Syndication;
using System.Xml;
using System.Globalization;

namespace YahooFinance
{
    /// <summary>
    /// Returns news items using Yahoo Finance (HTML page based)
    /// </summary>
    public class YahooHtmlNewsDataService : INewsDataService
    {
        /// <summary>
        /// Returns a list of news items for the given symbol
        /// </summary>
        /// <param name="symbol"></param>
        /// <returns></returns>
        public IList<NewsItem> GetNewsItemsFromSource(string symbol)
        {
            if(symbol == null) return null;
            List<NewsItem> newsItems = new List<NewsItem>();
            string url = "http://uk.finance.yahoo.com/q/h?s=" + symbol;
            System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            webRequest.Accept = "text/xml";
            using (System.Net.HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse())
            using (System.IO.Stream responseStream = webResponse.GetResponseStream())
            {
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.Load(responseStream);

                var h3s = doc.DocumentNode.SelectNodes("//div[@class='mod yfi_quote_headline withsky']/h3");
                if (h3s == null) return newsItems;
                CultureInfo provider = CultureInfo.InvariantCulture;
                foreach (var h3 in h3s)
                {
                    var span = h3.SelectSingleNode("span");
                    string header = span.InnerText.Replace("\r\n", String.Empty).Trim();
                    DateTimeOffset date = DateTimeOffset.MinValue;
                    try
                    {
                        date = new DateTimeOffset(DateTime.ParseExact(header, "dddd, d MMMM yyyy", provider), TimeSpan.FromHours(0)); //<span>Tuesday, 6 September 2011</span>
                    }
                    catch (FormatException) { }

                    var ul = h3.NextSibling.NextSibling;//.SelectSingleNode(".//ul");

                    var lis = ul.SelectNodes("li");
                    if (lis == null) continue;
                    foreach (var li in lis)
                    {
                        var anchor = li.SelectSingleNode("a");
                        if (anchor == null) continue;
                        var cite = li.SelectSingleNode("cite");
                        var citeText = HtmlAgilityPack.HtmlEntity.DeEntitize(cite.InnerText)
                            .Replace("\r\n", String.Empty)
                            .Replace("\u00A0", " ")
                            .Trim();
                        NewsItem newsItem = new NewsItem();
                        newsItem.Headline = HtmlAgilityPack.HtmlEntity.DeEntitize(anchor.InnerText);
                        newsItem.DisplayHeadline = citeText
                            + " - "
                            + newsItem.Headline;
                        newsItem.Url = anchor.Attributes["href"].Value;
                        newsItem.Date = date;
                        newsItems.Add(newsItem);
                    }
                }
            }
            return newsItems;
        }
    }
}
